package service

import (
	"ksd-social-api/commons/base/service"
	cmodel "ksd-social-api/modules/special/model"
	"strconv"
)

type SpecialBindCodeService struct {
	service.BaseService
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 查询小册和code是否存在
 */
func (service *SpecialBindCodeService) DuihuanSpecialBind(code string, specialid uint64, bindId uint64, userId uint64, systemId uint64, systemName string) bool {
	specialCode := userBuySpecialBindMapper.CountSpecialBindCode(code, specialid, bindId, systemId)
	// 如果还没兑换，就开始兑换，生成小册订单，同时删除订单
	if specialCode > 0 {
		// 删除兑换的订单
		userBuySpecialBindMapper.UpdateSpecialBindCode(code, specialid, bindId, systemId)
		// 开始保存订单
		service.saveUserSpecialBindOrder(code, specialid, bindId, userId, systemId, systemName)
		// 兑换成功
		return true
	}
	return false
}

/**
 * 保存用户小册订单
 * @author feige
 * @date 2023-12-06
 * @version 1.0
 * @desc
 */
func (service *SpecialBindCodeService) saveUserSpecialBindOrder(code string, specialId uint64, bindId uint64, userId uint64, systemId uint64, systemName string) {
	// 1： 判断用户是否购买此小册，如果购买过了就就不要去保存订单
	isBuyFlag := userBuySpecialBindMapper.CountUserBuySpecialBindNo(userId, specialId, bindId, systemId)
	if isBuyFlag == 0 {
		// 根据小册id查询小册信息
		special, _ := specialMapper.GetByIdTOPay(specialId, systemId)
		userVo := userMapper.GetUserByID(userId, systemId)

		userBuySpecialBind := cmodel.SpecialBindUserBuy{}
		OutTradeNo := service.GetSnowWorkerIdString(8)
		// 处理返回结果
		userBuySpecialBind.Opid = bindId
		userBuySpecialBind.SpecialId = specialId
		userBuySpecialBind.Title = special.Title
		userBuySpecialBind.Cover = special.Img

		userBuySpecialBind.UserId = userId
		userBuySpecialBind.Username = userVo.Username
		userBuySpecialBind.Avatar = userVo.Avatar
		userBuySpecialBind.Uuid = userVo.Uuid
		userBuySpecialBind.Phone = userVo.Telephone
		userBuySpecialBind.Address = userVo.Address
		userBuySpecialBind.Nickname = userVo.Nickname

		userBuySpecialBind.Code = code
		userBuySpecialBind.PayMethod = 3
		userBuySpecialBind.PayMethodName = "兑换码"
		userBuySpecialBind.Tradeno = OutTradeNo
		userBuySpecialBind.Orderno = OutTradeNo
		userBuySpecialBind.OrderJson = ""
		userBuySpecialBind.SystemId = systemId
		userBuySpecialBind.SystemName = systemName
		userBuySpecialBind.Price = special.Price
		// 保存订单的接口
		userBuySpecialBindMapper.SaveUserBuySpecialBind(userBuySpecialBind)
		// 开始查询所有的订单的金额是否大于专题订购的总价，自动开通所有权限
		sumBindPrice := userBuySpecialBindMapper.SumUserBuySpecialBindPrice(userId, specialId, systemId)
		if len(sumBindPrice) > 0 {
			Price, _ := strconv.Atoi(sumBindPrice)
			SPrice, _ := strconv.Atoi(special.Price)
			if Price >= SPrice {
				userSpecialService.SaveUserSpecial(OutTradeNo, OutTradeNo, "", 3, "兑换码", special.Price,
					userId, specialId, systemId, systemName)
			}
		}

	}
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 查询小册和code是否存在
 */
func (service *SpecialBindCodeService) DuihuanCoinSpecialBind(specialId uint64, bindId uint64, userId uint64, systemId uint64, systemName string) bool {
	// 根据小册id查询小册信息
	special, _ := specialMapper.GetByIdTOPay(specialId, systemId)
	userVo := userMapper.GetUserByID(userId, systemId)
	price, _ := strconv.Atoi(special.Price)
	if userVo.Coin <= 0 || price > userVo.Coin {
		return false
	}
	// 1： 判断用户是否购买此小册，如果购买过了就就不要去保存订单
	isBuyFlag := userBuySpecialBindMapper.CountUserBuySpecialBindNo(userId, specialId, bindId, systemId)
	if isBuyFlag == 0 {
		userBuySpecialBind := cmodel.SpecialBindUserBuy{}
		OutTradeNo := service.GetSnowWorkerIdString(10)
		// 处理返回结果
		userBuySpecialBind.Opid = bindId
		userBuySpecialBind.SpecialId = specialId
		userBuySpecialBind.Title = special.Title
		userBuySpecialBind.Cover = special.Img
		userBuySpecialBind.UserId = userId
		userBuySpecialBind.Username = userVo.Username
		userBuySpecialBind.Avatar = userVo.Avatar
		userBuySpecialBind.Uuid = userVo.Uuid
		userBuySpecialBind.Phone = userVo.Telephone
		userBuySpecialBind.Address = userVo.Address
		userBuySpecialBind.Nickname = userVo.Nickname

		userBuySpecialBind.Code = ""
		userBuySpecialBind.PayMethod = 4
		userBuySpecialBind.PayMethodName = "学习币"
		userBuySpecialBind.Tradeno = OutTradeNo
		userBuySpecialBind.Orderno = OutTradeNo
		userBuySpecialBind.OrderJson = ""
		userBuySpecialBind.SystemId = systemId
		userBuySpecialBind.SystemName = systemName
		userBuySpecialBind.Price = special.Price
		// 保存订单的接口
		userBuySpecialBindMapper.SaveUserBuySpecialBindCoin(userBuySpecialBind)

		// 开始查询所有的订单的金额是否大于专题订购的总价，自动开通所有权限
		sumBindPrice := userBuySpecialBindMapper.SumUserBuySpecialBindPrice(userId, specialId, systemId)
		if len(sumBindPrice) > 0 {
			Price, _ := strconv.Atoi(sumBindPrice)
			SPrice, _ := strconv.Atoi(special.Price)
			if Price >= SPrice {
				userSpecialService.SaveUserSpecial(OutTradeNo, OutTradeNo, "", 4, "学习币", special.Price,
					userId, specialId, systemId, systemName)
			}
		}

	}
	// 兑换成功
	return true
}
